﻿package net.htjs.mobile.android.crm;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.htjs.mobile.android.crm.db.DatabaseHandler;
import net.htjs.mobile.android.crm.util.L;
import android.app.Activity;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.CacheManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainLayout {
	private Activity activity;
	private int layoutWidth = 0;//屏幕宽度
	
	private String [][]menuItems = null ;//滑动菜单的数据结构
	private ArrayList<View> menuList=null;//存滑动菜单的大项
	private int menuIndex=0;//滑动菜单大项索引
	private ArrayList<View> itemList=null;//存滑动菜单的所有小项
	private int itemIndex=0;//滑动菜单小项索引
	private int itemCount=0;//滑动菜单总项数
	private ViewPager menuPager = null;
	private ImageView imagePrivious = null;
	private ImageView imageNext = null;
	
	private String [] contentItems = null;
	public ArrayList<MyWebView> contentList = null;
	private int contentIndex = 0;
	private MyViewPager contentPager = null;
	private LinearLayout linerLayout_content = null;
	private String []notificationItems=null;//提醒数字
	private TextView tv=null;
	private int[][] img4Menu;
	
	LayoutInflater mInflater;
	DatabaseHandler dbh;
	//构造函数
	public MainLayout(Activity activity , String [][]menuItems , String []contentItems , String []notificationItems,int layoutWidth , LayoutInflater mInflater , int[][] img4Menu , DatabaseHandler dbh){
		this.activity = activity;
		this.layoutWidth = layoutWidth;
		
		this.menuItems = menuItems;
		this.menuList = new ArrayList<View>();
		this.itemList = new ArrayList<View>();
		this.menuPager = (ViewPager)activity.findViewById(R.id.bottom_view);
		this.imagePrivious = (ImageView)activity.findViewById(R.id.ivPreviousButton);
		this.imageNext = (ImageView)activity.findViewById(R.id.ivNextButton);
		this.imagePrivious.setOnClickListener(new imagePerviousOnClickListener());
		this.imageNext.setOnClickListener(new imageNextOnClickListener());
		
		this.contentItems = contentItems;
		this.contentList = new ArrayList<MyWebView>();
		this.contentPager = new MyViewPager(activity);
		this.contentPager.setOnTouchListener(contentPager);
		this.linerLayout_content = (LinearLayout)activity.findViewById(R.id.LinerLayout_content);
		LinearLayout.LayoutParams linearLayoutContentParams= new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		this.linerLayout_content.addView(contentPager,linearLayoutContentParams);
		
		this.notificationItems=notificationItems;
		this.tv=(TextView)activity.findViewById(R.id.textView01);
		
		this.mInflater=mInflater;
		this.img4Menu=img4Menu;
		this.dbh=dbh;
	}
	
	//创建滑动菜单对象
	public void setMenuPager(){
		for(int i=0;i<menuItems.length;i++){
        	menuList.add(this.getSliderMenuLayout(menuItems[i],this.img4Menu[i]));
        }
		menuPager.setAdapter(new SlideMenuAdapter(menuList));//设置滑动菜单适配器
		menuPager.setOnPageChangeListener(new slideMenuChangeListener());//设置滑动菜单滑动监听器
	}
	
	//获取滑动菜单对象
	public ViewPager getMenuPager(){
		return this.menuPager;
	}
	
	//设置滑动菜单的当前页数
	public void setMenuIndex(int menuIndex){
		this.menuIndex = menuIndex;
		menuPager.setCurrentItem(menuIndex);
	}
	//设置滑动菜单的当前项数
	public void setItemIndex(int itemIndex){
		this.itemIndex=itemIndex;
		this.setMenuIndex(itemIndex/4);
		for(int i=0;i<itemList.size();i++){
			if(i==itemIndex){
				itemList.get(i).setBackgroundResource(R.drawable.menu_bg);
			}else{
				itemList.get(i).setBackgroundDrawable(null);
			}
		}
	}
	//创建滑动菜单的每一子项
	public View getSliderMenuLayout(String [] menuTextViews,int[] image4Menu){
		LinearLayout menuLinearLayout = new LinearLayout(activity);
		menuLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		// 参数设置
		LinearLayout.LayoutParams menuLinearLayoutParames = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT, 
				LinearLayout.LayoutParams.WRAP_CONTENT,
				1);
		menuLinearLayoutParames.gravity = Gravity.CENTER_HORIZONTAL;
		
		// 添加TextView控件
		for(int i = 0;i < menuTextViews.length; i++){
			
			
			View view = mInflater.inflate(R.layout.menuitem, null);
			
			TextView tvMenu =(TextView) view.findViewById(R.id.ItemText);
			// 设置标识值
			
			tvMenu.setText(menuTextViews[i]);
			/*tvMenu.setWidth(layoutWidth / 4);
			tvMenu.setHeight(80);
			tvMenu.setPadding(30, 14, 30, 10);
			
			tvMenu.setTextColor(Color.WHITE);
			tvMenu.setBackgroundResource(R.drawable.car);*/
			//添加点击事件
			view.setTag(menuTextViews[i]);//这句话不能少
			view.setOnClickListener(SlideMenuOnClickListener);
			
			ImageView imgView = (ImageView) view.findViewById(R.id.ItemImage);
			imgView.setImageResource(image4Menu[i]);
			

			if(L.notificationNum.get(this.notificationItems[itemCount])!=null){
			BadgeView bv=new BadgeView(activity,imgView);
			bv.setText(L.notificationNum.get(this.notificationItems[itemCount]));
			bv.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
			bv.show();
			}
			
			menuLinearLayout.addView(view,menuLinearLayoutParames);
			itemCount+=1;
			itemList.add(view);
		}
		
		return menuLinearLayout;
	}
	
    //滑动菜单适配器
    class SlideMenuAdapter extends PagerAdapter{
    	private List<View> list;

    	public SlideMenuAdapter(List<View> list){
    		this.list=list;
    	}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		
		 @Override  
	    public int getItemPosition(Object object) {  
	        // TODO Auto-generated method stub  
	        return super.getItemPosition(object);  
	    }
		
		 @Override  
	    public void destroyItem(View arg0, int arg1, Object arg2) {  
	        // TODO Auto-generated method stub  
	        ((ViewPager) arg0).removeView(list.get(arg1));  
	    }  
	  
	    @Override  
	    public Object instantiateItem(View arg0, int arg1) {  
	        // TODO Auto-generated method stub  
	     ((ViewPager) arg0).addView(list.get(arg1));
	            
	        return list.get(arg1);  
	    }  
	  
	    @Override  
	    public void restoreState(Parcelable arg0, ClassLoader arg1) {  
	        // TODO Auto-generated method stub  
	  
	    }  
	  
	    @Override  
	    public Parcelable saveState() {  
	        // TODO Auto-generated method stub  
	        return null;  
	    }  
	  
	    @Override  
	    public void startUpdate(View arg0) {  
	        // TODO Auto-generated method stub  
	  
	    }  
	  
	    @Override  
	    public void finishUpdate(View arg0) {  
	        // TODO Auto-generated method stub  
	  
	    }   
    	
    }
    
  
    //下一页
    class imageNextOnClickListener implements OnClickListener{
    	public void onClick(View v) {
    		menuIndex++;
    		menuPager.setCurrentItem(menuIndex);
    	}
    }
    
    //上一页
    class imagePerviousOnClickListener implements OnClickListener{

		public void onClick(View v) {
			menuIndex--;
			menuPager.setCurrentItem(menuIndex);
		}
    	
    }
    
    //滑动菜单改变事件，菜单滑动时，改变左移图片和右移图片的显示
    class slideMenuChangeListener implements OnPageChangeListener{

		public void onPageScrollStateChanged(int arg0) {
			
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
			
		}

		public void onPageSelected(int page) {
			menuIndex = page;
			int pageCount = menuList.size();
			//显示左移图片
			if(menuIndex > 0 && menuIndex <=pageCount-1 ){
				imagePrivious.setVisibility(View.VISIBLE);
			}else{
				imagePrivious.setVisibility(View.INVISIBLE);
			}
			//显示右移图片
			if(menuIndex >= 0 && menuIndex < pageCount-1 ){
				imageNext.setVisibility(View.VISIBLE);
			}else{
				imageNext.setVisibility(View.INVISIBLE);
			}
		}
    	
    }
	
	//定义菜单点击事件，点击时改变点击菜单的背景，同时改变WebView的内容
    OnClickListener SlideMenuOnClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String menuTag = v.getTag().toString();
			
			if(v.isClickable()){
				v.setBackgroundResource(R.drawable.menu_bg);
				
				for(int i = 0;i < itemList.size();i++){
					if(!menuTag.equals(itemList.get(i).getTag().toString())){
						itemList.get(i).setBackgroundDrawable(null);
					}else{
						MainLayout.this.setContentIndex(i);//改变WebView的内容
						tv.setText(getText(notificationItems[i]));
					}
				}
				
			}
		}
	};
	
	
	
	//设置滑动的内容
    public void setContentPager(){
		for(int i = 0;i < contentItems.length;i++){
			MyWebView webView = new MyWebView(activity);
			File file = CacheManager.getCacheFileBaseDir();
			String[] a=file.list();
	        
			webView.setOnTouchListener(webView);
			webView.getSettings().setJavaScriptEnabled(true);
			webView.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
			webView.setWebViewClient(new MyWebClient(dbh));
			contentList.add(webView);
		}
		contentPager.setAdapter(new SlideContentAdapter(contentList));//设置滑动内容适配器
		contentPager.setOnPageChangeListener(new SlideContentChangeListener());//设置滑动内容滑动监听器
	}
	
	public MyViewPager getContentPager(){
		return this.contentPager;
	}
	
	//滑动滑动内容适配器
    class SlideContentAdapter extends PagerAdapter{
    	private List<MyWebView> list;
    	
    	public SlideContentAdapter(List<MyWebView> list){
    		this.list=list;
    	}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		
		 @Override  
	    public int getItemPosition(Object object) {  
	        // TODO Auto-generated method stub  
	        return super.getItemPosition(object);  
	    }
		
		 @Override  
	    public void destroyItem(View arg0, int arg1, Object arg2) {  
	        // TODO Auto-generated method stub  
	        ((ViewPager) arg0).removeView(list.get(arg1));  
	    }  
	  
	    @Override  
	    public Object instantiateItem(View arg0, int arg1) {  
	        // TODO Auto-generated method stub  
	     ((ViewPager) arg0).addView(list.get(arg1));
	            
	        return list.get(arg1);  
	    }  
	  
	    @Override  
	    public void restoreState(Parcelable arg0, ClassLoader arg1) {  
	        // TODO Auto-generated method stub  
	  
	    }  
	  
	    @Override  
	    public Parcelable saveState() {  
	        // TODO Auto-generated method stub  
	        return null;  
	    }  
	  
	    @Override  
	    public void startUpdate(View arg0) {  
	        // TODO Auto-generated method stub  
	  
	    }  
	  
	    @Override  
	    public void finishUpdate(View arg0) {  
	        // TODO Auto-generated method stub  
	  
	    }   
    	
    }
    
	//设置滑动内容WebView的当前URL
	public void setContentIndex(int contentIndex){
		this.contentIndex=contentIndex;
		contentList.get(this.contentIndex).loadUrl(contentItems[this.contentIndex]);
		contentPager.setCurrentItem(this.contentIndex);
		tv.setText(getText(notificationItems[contentIndex]));
		//((WebView)contentPager.getChildAt(index)).loadUrl(contentItems[this.contentIndex]);
		
	}
	
	public int getContentIndex(){
		return contentPager.getCurrentItem();
	}
	class SlideContentChangeListener implements OnPageChangeListener{

		public void onPageScrollStateChanged(int arg0) {
			
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
			if(arg0==1){
				Log.i("tag", "page scrolling");
			}
		}

		public void onPageSelected(int page) {
			contentIndex=page;
			MainLayout.this.setContentIndex(page);
			MainLayout.this.setItemIndex(page);
			tv.setText(getText(notificationItems[page]));
		}
		
	}
	
	//获取标题的名字
	private String getText(String item){
		String text=null;
		if(item.equals("kqsp")){
			text="考评审批";
		}else if(item.equals("wssp")){
			text="网上审批";
		}else if(item.equals("xtbg")){
			text="协调办公";
		}else if(item.equals("pgfw")){
			text="我的派工";
		}
		return text;
	}
}
